The delete
operator expects a pointer argument. Passing an object to it may compile and seem to run (with an implicit cast to pointer
type), but it can result in unexpected behavior at runtime.
Noncompliant code example
class CString {
public:
operator const char*();
// ...
};
void fun() {
CString str;
// ...
delete str; // Noncompliant
}
Compliant solution
class CString {
public:
operator const char*();
// ...
};
void fun() {
CString *pstr = new CString;
// ...
delete pstr;
}